/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * Desktop.java
 *
 * Created on 29/04/2011, 17:05:10
 */
package Interface;


import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import ludi.Usuario;
import utils.BancoDeDados;


/**
 *
 * @author alison
 */
public class Desktop extends javax.swing.JFrame {


    BancoDeDados banco;
    Usuario usuario;

    public Desktop(BancoDeDados banco, Usuario usuario) throws SQLException {

        initComponents();
        this.banco = banco;
        if (usuario.getPermissoes()==2){
                menuCadastrarUsuario.setVisible(false);
                menuCadastrarModalidade.setVisible(false);
                menuConsultarUsuario.setVisible(false);
                this.usuario = banco.getCurso(usuario.getLogin());
        } else {
            this.usuario = usuario;
        }
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setVisible(true);
        ajuda.doClick();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopArea = new Interface.DesktopArea();
        barraMenu = new javax.swing.JMenuBar();
        menuCadastrar = new javax.swing.JMenu();
        menuCadastrarUsuario = new javax.swing.JMenuItem();
        menuCadastrarModalidade = new javax.swing.JMenuItem();
        menuCadastrarTime = new javax.swing.JMenuItem();
        menuCadastrarAtleta = new javax.swing.JMenuItem();
        menuConsultar = new javax.swing.JMenu();
        menuConsultarUsuario = new javax.swing.JMenuItem();
        menuConsultarModalidade = new javax.swing.JMenuItem();
        menuConsultarTime = new javax.swing.JMenuItem();
        menuConsultarAtleta = new javax.swing.JMenuItem();
        menusAjuda = new javax.swing.JMenu();
        ajuda = new javax.swing.JMenuItem();
        menuSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LUDi 1.0");
        setMinimumSize(new java.awt.Dimension(800, 600));

        desktopArea.setBackground(new java.awt.Color(249, 249, 249));

        barraMenu.setFont(barraMenu.getFont().deriveFont(barraMenu.getFont().getStyle() | java.awt.Font.BOLD));

        menuCadastrar.setText("Cadastrar");

        menuCadastrarUsuario.setText("Usuário");
        menuCadastrarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCadastrarUsuarioActionPerformed(evt);
            }
        });
        menuCadastrar.add(menuCadastrarUsuario);

        menuCadastrarModalidade.setText("Modalidade");
        menuCadastrarModalidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCadastrarModalidadeActionPerformed(evt);
            }
        });
        menuCadastrar.add(menuCadastrarModalidade);

        menuCadastrarTime.setText("Time");
        menuCadastrarTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCadastrarTimeActionPerformed(evt);
            }
        });
        menuCadastrar.add(menuCadastrarTime);

        menuCadastrarAtleta.setText("Atleta");
        menuCadastrarAtleta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCadastrarAtletaActionPerformed(evt);
            }
        });
        menuCadastrar.add(menuCadastrarAtleta);

        barraMenu.add(menuCadastrar);

        menuConsultar.setText("Consultar");

        menuConsultarUsuario.setText("Usuários Cadastrados");
        menuConsultarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuConsultarUsuarioActionPerformed(evt);
            }
        });
        menuConsultar.add(menuConsultarUsuario);

        menuConsultarModalidade.setText("Modalidades Cadastradas");
        menuConsultarModalidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuConsultarModalidadeActionPerformed(evt);
            }
        });
        menuConsultar.add(menuConsultarModalidade);

        menuConsultarTime.setText("Times Cadastrados");
        menuConsultarTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuConsultarTimeActionPerformed(evt);
            }
        });
        menuConsultar.add(menuConsultarTime);

        menuConsultarAtleta.setText("Atletas Cadastrados");
        menuConsultarAtleta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuConsultarAtletaActionPerformed(evt);
            }
        });
        menuConsultar.add(menuConsultarAtleta);

        barraMenu.add(menuConsultar);

        menusAjuda.setText("Ajuda");

        ajuda.setText("Exibir tela de boas-vindas");
        ajuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ajudaActionPerformed(evt);
            }
        });
        menusAjuda.add(ajuda);

        menuSobre.setText("Sobre LUDI");
        menuSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSobreActionPerformed(evt);
            }
        });
        menusAjuda.add(menuSobre);

        barraMenu.add(menusAjuda);

        setJMenuBar(barraMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopArea, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopArea, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void menuSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSobreActionPerformed
        SobreLudi sobre = new SobreLudi(this, true);
    }//GEN-LAST:event_menuSobreActionPerformed


    private void menuCadastrarModalidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCadastrarModalidadeActionPerformed
        CriarModalidade frame = new CriarModalidade(banco, null);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuCadastrarModalidadeActionPerformed


    private void menuCadastrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCadastrarUsuarioActionPerformed
        CriarUsuario frame = new CriarUsuario(banco, usuario, null);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuCadastrarUsuarioActionPerformed


    private void menuCadastrarTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCadastrarTimeActionPerformed
        try {
            if (banco.getListaDeCursos().size() > 0) {
                if (banco.getListaDeModalidades(null, null).size()>0){
                    CriarTime frame = new CriarTime(banco, usuario, null);
                    frame.setVisible(true);
                    desktopArea.add(frame);
                    frame.setSelected(true);
                } else{
                    JOptionPane.showMessageDialog(this, "Você não pode cadastrar times.\nNão há modalidades cadastradas!", "LUDi", JOptionPane.INFORMATION_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "Você não pode cadastrar times.\nNão há cursos cadastrados!", "LUDi", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_menuCadastrarTimeActionPerformed


    private void menuCadastrarAtletaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCadastrarAtletaActionPerformed
        try {
            if (banco.getListaDeCursos().size() > 0) {
                CriarAtleta frame = new CriarAtleta(banco, usuario, null);
                frame.setVisible(true);
                desktopArea.add(frame);
                frame.setSelected(true);
            } else {
                JOptionPane.showMessageDialog(this, "Você não pode cadastrar atletas.\nNão há cursos cadastrados!", "LUDi", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_menuCadastrarAtletaActionPerformed


    private void menuConsultarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuConsultarUsuarioActionPerformed
        ConsultarUsuarios frame = new ConsultarUsuarios(banco, usuario);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuConsultarUsuarioActionPerformed


    private void menuConsultarModalidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuConsultarModalidadeActionPerformed
        ConsultarModalidades frame = new ConsultarModalidades(banco, usuario);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuConsultarModalidadeActionPerformed


    private void menuConsultarTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuConsultarTimeActionPerformed
        ConsultarTime frame = new ConsultarTime(banco, usuario);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuConsultarTimeActionPerformed


    private void menuConsultarAtletaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuConsultarAtletaActionPerformed
        ConsultarAtletas frame = new ConsultarAtletas(banco, usuario);
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_menuConsultarAtletaActionPerformed

    private void ajudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ajudaActionPerformed
        Ajuda frame = new Ajuda();
        frame.setVisible(true);
        desktopArea.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }//GEN-LAST:event_ajudaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ajuda;
    private javax.swing.JMenuBar barraMenu;
    private Interface.DesktopArea desktopArea;
    private javax.swing.JMenu menuCadastrar;
    private javax.swing.JMenuItem menuCadastrarAtleta;
    private javax.swing.JMenuItem menuCadastrarModalidade;
    private javax.swing.JMenuItem menuCadastrarTime;
    private javax.swing.JMenuItem menuCadastrarUsuario;
    private javax.swing.JMenu menuConsultar;
    private javax.swing.JMenuItem menuConsultarAtleta;
    private javax.swing.JMenuItem menuConsultarModalidade;
    private javax.swing.JMenuItem menuConsultarTime;
    private javax.swing.JMenuItem menuConsultarUsuario;
    private javax.swing.JMenuItem menuSobre;
    private javax.swing.JMenu menusAjuda;
    // End of variables declaration//GEN-END:variables
}